package com.idlecode.panel.tool;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.ui.Messages;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-11-19 08:52
 **/
public class TimestampPanel extends JPanel {

    private JTextField timestampMillisecondTextField;
    private JTextField timestampSecondTextField;

    private JTextField dateTimeYMDHMSSTextField;
    private JTextField dateTimeYMDHMSTextField;
    private JTextField dateTimeYMDHMTextField;
    private JTextField dateTimeYMDHTextField;
    private JTextField dateTimeYMDTextField;

    private JTextField dateTimeYMDHMSSNTextField;
    private JTextField dateTimeYMDHMSNTextField;

    private SimpleDateFormat sdf_yMdHmsS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private SimpleDateFormat sdf_yMdHms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat sdf_yMdHm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private SimpleDateFormat sdf_yMdH = new SimpleDateFormat("yyyy-MM-dd HH");
    private SimpleDateFormat sdf_yMd = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat sdf_yMdHmsS_n = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private SimpleDateFormat sdf_yMdHms_n = new SimpleDateFormat("yyyyMMddHHmmss");

    private Map<String, String> timeZoneMap = new HashMap<>();

    private String[] timeZoneOptions = {"中国/北京 (+08)", "中国/新疆 (+06)", "日本/东京 (JST)", "泰国/曼谷 (+07)", "韩国/首尔 (KST)",
            "朝鲜/平壤 (KST)", "新加坡 (+08)", "迪拜 (+04)", "俄罗斯/海参崴 (+10)", "尼泊尔 (+0545)", "印度 (IST)",
            "埃及/开罗 (EET)", "美国/纽约 (EDT)", "美国/芝加哥 (CDT)", "美国/夏威夷 (-10)", "美国/底特律 (EDT)",
            "加拿大/多伦多 (EDT)", "巴西/圣保罗 (-03)", "巴拿马 (EST)", "墨西哥 (CDT)", "澳大利亚 (+10)", "英国/伦敦 (00)",
            "德国/柏林 (+01)", "法国/巴黎(+01)", "意大利/罗马(+01)", "葡萄牙 (00)", "西班牙 (+01)", "零时区(+00)",
            "东一区(+01)", "东二区(+02)", "东三区(+03)", "东四区(+04)", "东五区(+05)", "东六区(+06)", "东七区(+07)",
            "东八区(+08)", "东九区(+09)", "东十区(+10)", "东十一区(+11)", "东十二区(+12)", "西一区(-01)", "西二区(-02)",
            "西三区(-03)", "西四区(-04)", "西五区(-05)", "西六区(-06)", "西七区(-07)", "西八区(-08)", "西九区(-09)",
            "西十区(-10)", "西十一区(-11)", "西十二区(-12)"};

    private Timer timer; // 定时器
    private boolean isPaused = true; // 用于控制暂停和继续

    public TimestampPanel() {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        // 时间戳-毫秒
        JPanel timestampMillisecondPanel = new JPanel();
        timestampMillisecondPanel.setLayout(new BoxLayout(timestampMillisecondPanel, BoxLayout.X_AXIS));
        timestampMillisecondPanel.add(Box.createRigidArea(new Dimension(20, 0)));
        timestampMillisecondPanel.add(new JLabel("当前时间："));
        timestampMillisecondTextField = new JTextField();
        timestampMillisecondTextField.setPreferredSize(new Dimension(160, 30));
        timestampMillisecondTextField.setMinimumSize(new Dimension(160, 30));
        timestampMillisecondTextField.setMaximumSize(new Dimension(160, 30));
        timestampMillisecondTextField.setEditable(false); // 设置为不可编辑
        timestampMillisecondPanel.add(timestampMillisecondTextField);
        timestampMillisecondPanel.add(new JLabel("毫秒"));
        timestampMillisecondPanel.add(Box.createRigidArea(new Dimension(20, 0)));
        JButton copyTimestampMillisecondButton = new JButton("复制");
        timestampMillisecondPanel.add(copyTimestampMillisecondButton);
        timestampMillisecondPanel.add(Box.createHorizontalGlue());
        this.add(timestampMillisecondPanel);

        // 时间戳-秒
        JPanel timestampSecondPanel = new JPanel();
        timestampSecondPanel.setLayout(new BoxLayout(timestampSecondPanel, BoxLayout.X_AXIS));
        timestampSecondPanel.add(Box.createRigidArea(new Dimension(80, 0)));
        timestampSecondTextField = new JTextField();
        timestampSecondTextField.setPreferredSize(new Dimension(160, 30));
        timestampSecondTextField.setMinimumSize(new Dimension(160, 30));
        timestampSecondTextField.setMaximumSize(new Dimension(160, 30));
        timestampSecondTextField.setEditable(false); // 设置为不可编辑
        timestampSecondPanel.add(timestampSecondTextField);
        timestampSecondPanel.add(new JLabel("秒"));
        timestampSecondPanel.add(Box.createRigidArea(new Dimension(32, 0)));
        JButton copyTimestampSecondButton = new JButton("复制");
        timestampSecondPanel.add(copyTimestampSecondButton);
        timestampSecondPanel.add(Box.createHorizontalGlue());
        this.add(timestampSecondPanel);

        // 当前时间，yyyy-MM-dd HH:mm:ss.SSS
        JPanel dateTimeYMDHMSSPanel = new JPanel();
        dateTimeYMDHMSSPanel.setLayout(new BoxLayout(dateTimeYMDHMSSPanel, BoxLayout.X_AXIS));
        dateTimeYMDHMSSPanel.add(Box.createRigidArea(new Dimension(80, 0)));
        dateTimeYMDHMSSTextField = new JTextField();
        dateTimeYMDHMSSTextField.setPreferredSize(new Dimension(160, 30));
        dateTimeYMDHMSSTextField.setMinimumSize(new Dimension(160, 30));
        dateTimeYMDHMSSTextField.setMaximumSize(new Dimension(160, 30));
        dateTimeYMDHMSSTextField.setEditable(false); // 设置为不可编辑
        dateTimeYMDHMSSPanel.add(dateTimeYMDHMSSTextField);
        dateTimeYMDHMSSPanel.add(Box.createRigidArea(new Dimension(44, 0)));
        JButton copyDateTimeYMDHMSSButton = new JButton("复制");
        dateTimeYMDHMSSPanel.add(copyDateTimeYMDHMSSButton);
        dateTimeYMDHMSSPanel.add(Box.createHorizontalGlue());
        this.add(dateTimeYMDHMSSPanel);

        // 当前时间，yyyy-MM-dd HH:mm:ss
        JPanel dateTimeYMDHMSPanel = new JPanel();
        dateTimeYMDHMSPanel.setLayout(new BoxLayout(dateTimeYMDHMSPanel, BoxLayout.X_AXIS));
        dateTimeYMDHMSPanel.add(Box.createRigidArea(new Dimension(80, 0)));
        dateTimeYMDHMSTextField = new JTextField();
        dateTimeYMDHMSTextField.setPreferredSize(new Dimension(160, 30));
        dateTimeYMDHMSTextField.setMinimumSize(new Dimension(160, 30));
        dateTimeYMDHMSTextField.setMaximumSize(new Dimension(160, 30));
        dateTimeYMDHMSTextField.setEditable(false); // 设置为不可编辑
        dateTimeYMDHMSPanel.add(dateTimeYMDHMSTextField);
        dateTimeYMDHMSPanel.add(Box.createRigidArea(new Dimension(44, 0)));
        JButton copyDateTimeYMDHMSButton = new JButton("复制");
        dateTimeYMDHMSPanel.add(copyDateTimeYMDHMSButton);
        dateTimeYMDHMSPanel.add(Box.createHorizontalGlue());
        this.add(dateTimeYMDHMSPanel);

        // 当前时间，yyyy-MM-dd HH:mm
        JPanel dateTimeYMDHMPanel = new JPanel();
        dateTimeYMDHMPanel.setLayout(new BoxLayout(dateTimeYMDHMPanel, BoxLayout.X_AXIS));
        dateTimeYMDHMPanel.add(Box.createRigidArea(new Dimension(80, 0)));
        dateTimeYMDHMTextField = new JTextField();
        dateTimeYMDHMTextField.setPreferredSize(new Dimension(160, 30));
        dateTimeYMDHMTextField.setMinimumSize(new Dimension(160, 30));
        dateTimeYMDHMTextField.setMaximumSize(new Dimension(160, 30));
        dateTimeYMDHMTextField.setEditable(false); // 设置为不可编辑
        dateTimeYMDHMPanel.add(dateTimeYMDHMTextField);
        dateTimeYMDHMPanel.add(Box.createRigidArea(new Dimension(44, 0)));
        JButton copyDateTimeYMDHMButton = new JButton("复制");
        dateTimeYMDHMPanel.add(copyDateTimeYMDHMButton);
        dateTimeYMDHMPanel.add(Box.createHorizontalGlue());
        this.add(dateTimeYMDHMPanel);

        // 当前时间，yyyy-MM-dd HH
        JPanel dateTimeYMDHPanel = new JPanel();
        dateTimeYMDHPanel.setLayout(new BoxLayout(dateTimeYMDHPanel, BoxLayout.X_AXIS));
        dateTimeYMDHPanel.add(Box.createRigidArea(new Dimension(80, 0)));
        dateTimeYMDHTextField = new JTextField();
        dateTimeYMDHTextField.setPreferredSize(new Dimension(160, 30));
        dateTimeYMDHTextField.setMinimumSize(new Dimension(160, 30));
        dateTimeYMDHTextField.setMaximumSize(new Dimension(160, 30));
        dateTimeYMDHTextField.setEditable(false); // 设置为不可编辑
        dateTimeYMDHPanel.add(dateTimeYMDHTextField);
        dateTimeYMDHPanel.add(Box.createRigidArea(new Dimension(44, 0)));
        JButton copyDateTimeYMDHButton = new JButton("复制");
        dateTimeYMDHPanel.add(copyDateTimeYMDHButton);
        dateTimeYMDHPanel.add(Box.createHorizontalGlue());
        this.add(dateTimeYMDHPanel);

        // 当前时间，yyyy-MM-dd
        JPanel dateTimeYMDPanel = new JPanel();
        dateTimeYMDPanel.setLayout(new BoxLayout(dateTimeYMDPanel, BoxLayout.X_AXIS));
        dateTimeYMDPanel.add(Box.createRigidArea(new Dimension(80, 0)));
        dateTimeYMDTextField = new JTextField();
        dateTimeYMDTextField.setPreferredSize(new Dimension(160, 30));
        dateTimeYMDTextField.setMinimumSize(new Dimension(160, 30));
        dateTimeYMDTextField.setMaximumSize(new Dimension(160, 30));
        dateTimeYMDTextField.setEditable(false); // 设置为不可编辑
        dateTimeYMDPanel.add(dateTimeYMDTextField);
        dateTimeYMDPanel.add(Box.createRigidArea(new Dimension(44, 0)));
        JButton copyDateTimeYMDButton = new JButton("复制");
        dateTimeYMDPanel.add(copyDateTimeYMDButton);
        dateTimeYMDPanel.add(Box.createHorizontalGlue());
        this.add(dateTimeYMDPanel);

        // 当前时间，yyyyMMddHHmmssSSS
        JPanel dateTimeYMDHMSSNPanel = new JPanel();
        dateTimeYMDHMSSNPanel.setLayout(new BoxLayout(dateTimeYMDHMSSNPanel, BoxLayout.X_AXIS));
        dateTimeYMDHMSSNPanel.add(Box.createRigidArea(new Dimension(80, 0)));
        dateTimeYMDHMSSNTextField = new JTextField();
        dateTimeYMDHMSSNTextField.setPreferredSize(new Dimension(160, 30));
        dateTimeYMDHMSSNTextField.setMinimumSize(new Dimension(160, 30));
        dateTimeYMDHMSSNTextField.setMaximumSize(new Dimension(160, 30));
        dateTimeYMDHMSSNTextField.setEditable(false); // 设置为不可编辑
        dateTimeYMDHMSSNPanel.add(dateTimeYMDHMSSNTextField);
        dateTimeYMDHMSSNPanel.add(Box.createRigidArea(new Dimension(44, 0)));
        JButton copyDateTimeYMDHMSSNButton = new JButton("复制");
        dateTimeYMDHMSSNPanel.add(copyDateTimeYMDHMSSNButton);
        dateTimeYMDHMSSNPanel.add(Box.createHorizontalGlue());
        this.add(dateTimeYMDHMSSNPanel);

        // 当前时间，yyyyMMddHHmmss
        JPanel dateTimeYMDHMSNPanel = new JPanel();
        dateTimeYMDHMSNPanel.setLayout(new BoxLayout(dateTimeYMDHMSNPanel, BoxLayout.X_AXIS));
        dateTimeYMDHMSNPanel.add(Box.createRigidArea(new Dimension(80, 0)));
        dateTimeYMDHMSNTextField = new JTextField();
        dateTimeYMDHMSNTextField.setPreferredSize(new Dimension(160, 30));
        dateTimeYMDHMSNTextField.setMinimumSize(new Dimension(160, 30));
        dateTimeYMDHMSNTextField.setMaximumSize(new Dimension(160, 30));
        dateTimeYMDHMSNTextField.setEditable(false); // 设置为不可编辑
        dateTimeYMDHMSNPanel.add(dateTimeYMDHMSNTextField);
        dateTimeYMDHMSNPanel.add(Box.createRigidArea(new Dimension(44, 0)));
        JButton copyDateTimeYMDHMSNButton = new JButton("复制");
        dateTimeYMDHMSNPanel.add(copyDateTimeYMDHMSNButton);
        dateTimeYMDHMSNPanel.add(Box.createHorizontalGlue());
        this.add(dateTimeYMDHMSNPanel);

        // 时间暂停或继续
        JPanel timeOutOrPursuePanel = new JPanel();
        timeOutOrPursuePanel.setLayout(new BoxLayout(timeOutOrPursuePanel, BoxLayout.X_AXIS));
        timeOutOrPursuePanel.add(Box.createRigidArea(new Dimension(284, 0)));
        JButton timeOutOrPursueButton = new JButton("继续");
        timeOutOrPursuePanel.add(timeOutOrPursueButton);
        timeOutOrPursuePanel.add(Box.createHorizontalGlue());
        this.add(timeOutOrPursuePanel);

        JPanel timestampConversionPanel = createTimestampConversionPanel();
        this.add(timestampConversionPanel);

        // 初始化定时器
        initTimer();
        //初始化时区
        initTimeZoneMap();

        // 按钮事件监听
        timeOutOrPursueButton.addActionListener(e -> {
            if (isPaused) {
                timer.start();
                timeOutOrPursueButton.setText("暂停");
            } else {
                timer.stop();
                timeOutOrPursueButton.setText("继续");
            }
            isPaused = !isPaused;
        });

        // 复制按钮事件
        copyTimestampMillisecondButton.addActionListener(e -> {
            String timestampMillisecond = timestampMillisecondTextField.getText();
            copyToClipboard(timestampMillisecond);
        });

        copyTimestampSecondButton.addActionListener(e -> {
            String timestampSecond = timestampSecondTextField.getText();
            copyToClipboard(timestampSecond);
        });

        copyDateTimeYMDHMSSButton.addActionListener(e -> {
            String dateTime = dateTimeYMDHMSSTextField.getText();
            copyToClipboard(dateTime);
        });

        copyDateTimeYMDHMSButton.addActionListener(e -> {
            String dateTime = dateTimeYMDHMSTextField.getText();
            copyToClipboard(dateTime);
        });

        copyDateTimeYMDHMButton.addActionListener(e -> {
            String dateTime = dateTimeYMDHMTextField.getText();
            copyToClipboard(dateTime);
        });

        copyDateTimeYMDHButton.addActionListener(e -> {
            String dateTime = dateTimeYMDHTextField.getText();
            copyToClipboard(dateTime);
        });

        copyDateTimeYMDButton.addActionListener(e -> {
            String dateTime = dateTimeYMDTextField.getText();
            copyToClipboard(dateTime);
        });

        copyDateTimeYMDHMSSNButton.addActionListener(e -> {
            String dateTime = dateTimeYMDHMSSNTextField.getText();
            copyToClipboard(dateTime);
        });

        copyDateTimeYMDHMSNButton.addActionListener(e -> {
            String dateTime = dateTimeYMDHMSNTextField.getText();
            copyToClipboard(dateTime);
        });
    }

    private void initTimer() {
        timer = new Timer(1, e -> {
            //毫秒时间戳
            long timeMillis = System.currentTimeMillis();
            timestampMillisecondTextField.setText(String.valueOf(timeMillis));

            timestampSecondTextField.setText(String.valueOf(System.currentTimeMillis() / 1000));

            Date date = new Date(timeMillis);

            sdf_yMdHmsS.setTimeZone(TimeZone.getDefault());
            dateTimeYMDHMSSTextField.setText(sdf_yMdHmsS.format(date));

            sdf_yMdHms.setTimeZone(TimeZone.getDefault());
            dateTimeYMDHMSTextField.setText(sdf_yMdHms.format(date));

            sdf_yMdHm.setTimeZone(TimeZone.getDefault());
            dateTimeYMDHMTextField.setText(sdf_yMdHm.format(date));

            sdf_yMdH.setTimeZone(TimeZone.getDefault());
            dateTimeYMDHTextField.setText(sdf_yMdH.format(date));

            sdf_yMd.setTimeZone(TimeZone.getDefault());
            dateTimeYMDTextField.setText(sdf_yMd.format(date));

            sdf_yMdHmsS_n.setTimeZone(TimeZone.getDefault());
            dateTimeYMDHMSSNTextField.setText(sdf_yMdHmsS_n.format(date));

            sdf_yMdHms_n.setTimeZone(TimeZone.getDefault());
            dateTimeYMDHMSNTextField.setText(sdf_yMdHms_n.format(date));
        });
        //timer.start();
    }

    private void copyToClipboard(String text) {
        Toolkit.getDefaultToolkit().getSystemClipboard()
                .setContents(new StringSelection(text), null);
    }

    /**
     * 时间戳转换
     */
    private JPanel createTimestampConversionPanel () {
        JPanel timestampConversionPanel = new JPanel();
        timestampConversionPanel.setLayout(new BorderLayout());

        JPanel timestampConversionTitlePanel = createTitlePanel("时间戳转换");
        // 添加标题面板到主面板
        timestampConversionPanel.add(timestampConversionTitlePanel, BorderLayout.NORTH);

        JPanel timestampConversionContentPanel = new JPanel();
        timestampConversionContentPanel.setLayout(new BoxLayout(timestampConversionContentPanel, BoxLayout.Y_AXIS));

        JPanel blankPanel1 = new JPanel();
        blankPanel1.setLayout(new BoxLayout(blankPanel1, BoxLayout.X_AXIS));
        JLabel jLabel1 = new JLabel("");
        jLabel1.setMinimumSize(new Dimension(Integer.MAX_VALUE, 15));
        jLabel1.setPreferredSize(new Dimension(Integer.MAX_VALUE, 15));
        jLabel1.setMaximumSize(new Dimension(Integer.MAX_VALUE, 15));
        blankPanel1.add(jLabel1);
        timestampConversionContentPanel.add(blankPanel1);

        JPanel timestampToDateTimePanel = createTimestampToDateTimePanel();
        timestampConversionContentPanel.add(timestampToDateTimePanel);
        JPanel blankPanel2 = new JPanel();
        blankPanel2.setLayout(new BoxLayout(blankPanel2, BoxLayout.X_AXIS));
        JLabel jLabel2 = new JLabel("");
        jLabel2.setMinimumSize(new Dimension(Integer.MAX_VALUE, 20));
        jLabel2.setPreferredSize(new Dimension(Integer.MAX_VALUE, 20));
        jLabel2.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
        blankPanel2.add(jLabel2);
        timestampConversionContentPanel.add(blankPanel2);
        JPanel dateTimeToTimestampPanel = createDateTimeToTimestampPanel();
        timestampConversionContentPanel.add(dateTimeToTimestampPanel);

        timestampConversionPanel.add(timestampConversionContentPanel, BorderLayout.CENTER);
        return timestampConversionPanel;
    }

    /**
     * 获取时间
     */
    private JPanel createTimestampToDateTimePanel() {
        // 时间戳
        JPanel timestampPanel = new JPanel();
        timestampPanel.setLayout(new BoxLayout(timestampPanel, BoxLayout.X_AXIS));
        timestampPanel.add(Box.createRigidArea(new Dimension(20, 0)));
        JTextField timestampTextField = new JTextField("时间戳");
        timestampTextField.setToolTipText("时间戳");
        // 设置提示文本颜色为灰色
        timestampTextField.setForeground(Color.GRAY);
        // 添加焦点监听器
        timestampTextField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (timestampTextField.getText().contains("时间戳")) {
                    timestampTextField.setText("");
                    // 获取当前主题
                    EditorColorsScheme currentScheme = EditorColorsManager.getInstance().getGlobalScheme();
                    // 获取文本前景色
                    Color textColor = currentScheme.getDefaultForeground();
                    timestampTextField.setForeground(textColor);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (timestampTextField.getText().isEmpty()) {
                    timestampTextField.setForeground(Color.GRAY);
                    timestampTextField.setText("时间戳");
                }
            }
        });
        timestampTextField.setPreferredSize(new Dimension(160, 30));
        timestampTextField.setMinimumSize(new Dimension(160, 30));
        timestampTextField.setMaximumSize(new Dimension(160, 30));
        timestampPanel.add(timestampTextField);
        //timestampPanel.add(Box.createRigidArea(new Dimension(20, 0)));
        JButton conversionButton = new JButton("转换");
        timestampPanel.add(conversionButton);
        JTextField dateTimeTextField = new JTextField();
        dateTimeTextField.setPreferredSize(new Dimension(160, 30));
        dateTimeTextField.setMinimumSize(new Dimension(160, 30));
        dateTimeTextField.setMaximumSize(new Dimension(160, 30));
        dateTimeTextField.setEditable(false); // 设置为不可编辑
        timestampPanel.add(dateTimeTextField);

        JComboBox timeZoneComboBox = new JComboBox<>(timeZoneOptions);
        timeZoneComboBox.setMaximumSize(new Dimension(130, 30));
        timestampPanel.add(timeZoneComboBox);
        // 添加 ActionListener 监听选择事件
        timeZoneComboBox.addActionListener(e -> {
            dateTimeTextField.setText(null);
            String timestamp = timestampTextField.getText();
            if (StrUtil.isBlank(timestamp) || "时间戳".equals(timestamp)) {
                return;
            }
            try {
                String timeZoneID = timeZoneMap.get(timeZoneComboBox.getSelectedItem());
                if (timestamp.length() <= 10) {
                    sdf_yMdHms.setTimeZone(TimeZone.getTimeZone(timeZoneID));
                    String formattedDateTime = sdf_yMdHms.format(new Date(Long.parseLong(timestamp) * 1000));
                    dateTimeTextField.setText(formattedDateTime);
                } else {
                    sdf_yMdHmsS.setTimeZone(TimeZone.getTimeZone(timeZoneID));
                    String formattedDateTime = sdf_yMdHmsS.format(new Date(Long.parseLong(timestamp)));
                    dateTimeTextField.setText(formattedDateTime);
                }
            } catch (Exception ex) {
                Messages.showMessageDialog(ex.getMessage(), "错误", Messages.getErrorIcon());
            }
        });
        timestampPanel.add(Box.createHorizontalGlue());

        conversionButton.addActionListener(e -> {
            dateTimeTextField.setText(null);
            String timestamp = timestampTextField.getText();
            if (StrUtil.isBlank(timestamp) || "时间戳".equals(timestamp)) {
                return;
            }
            try {
                String timeZoneID = timeZoneMap.get(timeZoneComboBox.getSelectedItem());
                if (timestamp.length() <= 10) {
                    sdf_yMdHms.setTimeZone(TimeZone.getTimeZone(timeZoneID));
                    String formattedDateTime = sdf_yMdHms.format(new Date(Long.parseLong(timestamp) * 1000));
                    dateTimeTextField.setText(formattedDateTime);
                } else {
                    sdf_yMdHmsS.setTimeZone(TimeZone.getTimeZone(timeZoneID));
                    String formattedDateTime = sdf_yMdHmsS.format(new Date(Long.parseLong(timestamp)));
                    dateTimeTextField.setText(formattedDateTime);
                }
            } catch (Exception ex) {
                Messages.showMessageDialog(ex.getMessage(), "错误", Messages.getErrorIcon());
            }
        });
        return timestampPanel;
    }

    /**
     * 获取时间戳
     */
    private JPanel createDateTimeToTimestampPanel() {
        // 时间
        JPanel dateTimePanel = new JPanel();
        dateTimePanel.setLayout(new BoxLayout(dateTimePanel, BoxLayout.X_AXIS));
        dateTimePanel.add(Box.createRigidArea(new Dimension(20, 0)));
        JTextField dateTimeTextField = new JTextField("...ss或...ss.SSS");
        dateTimeTextField.setToolTipText("<html>yyyy-MM-dd HH:mm:ss<br>yyyy-MM-dd HH:mm:ss.SSS</html>");
        // 设置提示文本颜色为灰色
        dateTimeTextField.setForeground(Color.GRAY);
        // 添加焦点监听器
        dateTimeTextField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (dateTimeTextField.getText().contains("...ss或...ss.SSS")) {
                    dateTimeTextField.setText("");
                    // 获取当前主题
                    EditorColorsScheme currentScheme = EditorColorsManager.getInstance().getGlobalScheme();
                    // 获取文本前景色
                    Color textColor = currentScheme.getDefaultForeground();
                    dateTimeTextField.setForeground(textColor);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (dateTimeTextField.getText().isEmpty()) {
                    dateTimeTextField.setForeground(Color.GRAY);
                    dateTimeTextField.setText("...ss或...ss.SSS");
                }
            }
        });
        dateTimeTextField.setPreferredSize(new Dimension(160, 30));
        dateTimeTextField.setMinimumSize(new Dimension(160, 30));
        dateTimeTextField.setMaximumSize(new Dimension(160, 30));
        dateTimePanel.add(dateTimeTextField);
        JButton conversionButton = new JButton("转换");
        dateTimePanel.add(conversionButton);
        JTextField timestampTextField = new JTextField();
        timestampTextField.setPreferredSize(new Dimension(160, 30));
        timestampTextField.setMinimumSize(new Dimension(160, 30));
        timestampTextField.setMaximumSize(new Dimension(160, 30));
        timestampTextField.setEditable(false); // 设置为不可编辑
        dateTimePanel.add(timestampTextField);
        JComboBox timeZoneComboBox = new JComboBox<>(timeZoneOptions);
        timeZoneComboBox.setMaximumSize(new Dimension(130, 30));
        dateTimePanel.add(timeZoneComboBox);
        // 添加 ActionListener 监听选择事件
        timeZoneComboBox.addActionListener(e -> {
            timestampTextField.setText(null);
            String dateTime = dateTimeTextField.getText();
            if (StrUtil.isBlank(dateTime) || "...ss或...ss.SSS".equals(dateTime)) {
                return;
            }
            try {
                String timeZoneID = timeZoneMap.get(timeZoneComboBox.getSelectedItem());
                Date date = null;
                int mark = 0;
                try {
                    sdf_yMdHmsS.setTimeZone(TimeZone.getTimeZone(timeZoneID));
                    date = sdf_yMdHmsS.parse(dateTime);
                    mark = 1;
                } catch (Exception e1) {}
                if (date == null) {
                    try {
                        sdf_yMdHms.setTimeZone(TimeZone.getTimeZone(timeZoneID));
                        date = sdf_yMdHms.parse(dateTime);
                        mark = 2;
                    } catch (Exception e1) {}
                }

                if (date == null) {
                    throw new RuntimeException("格式错误!");
                }
                long timestamp = date.getTime();
                if (mark == 1) {
                    timestampTextField.setText(timestamp + "");
                } else {
                    timestampTextField.setText((timestamp/1000) + "");
                }
            } catch (Exception ex) {
                Messages.showMessageDialog(ex.getMessage(), "错误", Messages.getErrorIcon());
            }
        });
        dateTimePanel.add(Box.createHorizontalGlue());

        conversionButton.addActionListener(e -> {
            timestampTextField.setText(null);
            String dateTime = dateTimeTextField.getText();
            if (StrUtil.isBlank(dateTime) || "...ss或...ss.SSS".equals(dateTime)) {
                return;
            }
            try {
                String timeZoneID = timeZoneMap.get(timeZoneComboBox.getSelectedItem());
                Date date = null;
                int mark = 0;
                try {
                    sdf_yMdHmsS.setTimeZone(TimeZone.getTimeZone(timeZoneID));
                    date = sdf_yMdHmsS.parse(dateTime);
                    mark = 1;
                } catch (Exception e1) {}
                if (date == null) {
                    try {
                        sdf_yMdHms.setTimeZone(TimeZone.getTimeZone(timeZoneID));
                        date = sdf_yMdHms.parse(dateTime);
                        mark = 2;
                    } catch (Exception e1) {}
                }

                if (date == null) {
                    throw new RuntimeException("格式错误!");
                }
                long timestamp = date.getTime();
                if (mark == 1) {
                    timestampTextField.setText(timestamp + "");
                } else {
                    timestampTextField.setText((timestamp/1000) + "");
                }
            } catch (Exception ex) {
                Messages.showMessageDialog(ex.getMessage(), "错误", Messages.getErrorIcon());
            }
        });
        return dateTimePanel;
    }

    private JPanel createTitlePanel(String title) {
        // 创建带横线的标题
        JPanel titlePanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 创建标题标签
        JLabel titleLabel = new JLabel(title + " ");
        titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 12));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));

        // 添加标题标签
        gbc.gridx = 0;
        gbc.weightx = 0; // 不占用额外空间
        gbc.anchor = GridBagConstraints.CENTER; // 垂直居中
        titlePanel.add(titleLabel, gbc);

        // 创建分隔符
        JSeparator separator = new JSeparator();
        separator.setForeground(Color.GRAY);

        // 添加分隔符
        gbc.gridx = 1;
        gbc.weightx = 1; // 占用剩余空间
        gbc.anchor = GridBagConstraints.CENTER; // 垂直居中
        titlePanel.add(separator, gbc);
        return titlePanel;
    }

    private void initTimeZoneMap() {
        timeZoneMap.put("中国/北京 (+08)", "Asia/Shanghai");
        timeZoneMap.put("中国/新疆 (+06)", "Asia/Urumqi");
        timeZoneMap.put("日本/东京 (JST)", "Asia/Tokyo");
        timeZoneMap.put("泰国/曼谷 (+07)", "Asia/Bangkok");
        timeZoneMap.put("韩国/首尔 (KST)", "Asia/Seoul");
        timeZoneMap.put("朝鲜/平壤 (KST)", "Asia/Pyongyang");
        timeZoneMap.put("新加坡 (+08)", "Asia/Singapore");
        timeZoneMap.put("迪拜 (+04)", "Asia/Dubai");
        timeZoneMap.put("俄罗斯/海参崴 (+10)", "Asia/Vladivostok");
        timeZoneMap.put("尼泊尔 (+0545)", "Asia/Kathmandu");
        timeZoneMap.put("印度 (IST)", "Asia/Kolkata");
        timeZoneMap.put("埃及/开罗 (EET)", "Africa/Cairo");
        timeZoneMap.put("美国/纽约 (EDT)", "America/New_York");
        timeZoneMap.put("美国/芝加哥 (CDT)", "America/Chicago");
        timeZoneMap.put("美国/夏威夷 (-10)", "Pacific/Honolulu");
        timeZoneMap.put("美国/底特律 (EDT)", "America/Detroit");
        timeZoneMap.put("加拿大/多伦多 (EDT)", "America/Toronto");
        timeZoneMap.put("巴西/圣保罗 (-03)", "America/Sao_Paulo");
        timeZoneMap.put("巴拿马 (EST)", "America/Panama");
        timeZoneMap.put("墨西哥 (CDT)", "America/Mexico_City");
        timeZoneMap.put("澳大利亚 (+10)", "Australia/Sydney");
        timeZoneMap.put("英国/伦敦 (00)", "Europe/London");
        timeZoneMap.put("德国/柏林 (+01)", "Europe/Berlin");
        timeZoneMap.put("法国/巴黎(+01)", "Europe/Paris");
        timeZoneMap.put("意大利/罗马(+01)", "Europe/Rome");
        timeZoneMap.put("葡萄牙 (00)", "Europe/Lisbon");
        timeZoneMap.put("西班牙 (+01)", "Europe/Madrid");
        timeZoneMap.put("零时区(+00)", "Etc/UTC");
        timeZoneMap.put("东一区(+01)", "Etc/GMT-1");
        timeZoneMap.put("东二区(+02)", "Etc/GMT-2");
        timeZoneMap.put("东三区(+03)", "Etc/GMT-3");
        timeZoneMap.put("东四区(+04)", "Etc/GMT-4");
        timeZoneMap.put("东五区(+05)", "Etc/GMT-5");
        timeZoneMap.put("东六区(+06)", "Etc/GMT-6");
        timeZoneMap.put("东七区(+07)", "Etc/GMT-7");
        timeZoneMap.put("东八区(+08)", "Etc/GMT-8");
        timeZoneMap.put("东九区(+09)", "Etc/GMT-9");
        timeZoneMap.put("东十区(+10)", "Etc/GMT-10");
        timeZoneMap.put("东十一区(+11)", "Etc/GMT-11");
        timeZoneMap.put("东十二区(+12)", "Etc/GMT-12");
        timeZoneMap.put("西一区(-01)", "Etc/GMT+1");
        timeZoneMap.put("西二区(-02)", "Etc/GMT+2");
        timeZoneMap.put("西三区(-03)", "Etc/GMT+3");
        timeZoneMap.put("西四区(-04)", "Etc/GMT+4");
        timeZoneMap.put("西五区(-05)", "Etc/GMT+5");
        timeZoneMap.put("西六区(-06)", "Etc/GMT+6");
        timeZoneMap.put("西七区(-07)", "Etc/GMT+7");
        timeZoneMap.put("西八区(-08)", "Etc/GMT+8");
        timeZoneMap.put("西九区(-09)", "Etc/GMT+9");
        timeZoneMap.put("西十区(-10)", "Etc/GMT+10");
        timeZoneMap.put("西十一区(-11)", "Etc/GMT+11");
        timeZoneMap.put("西十二区(-12)", "Etc/GMT+12");
    }
}
